<?php

/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 * 
 */

/**
 * Class with methods to handle and decode incomming data returned from a
 * SOAP Connection
 *
 * @author Bendsoft
 */
class CamelotXmlReader {

    /**
     * The sorted object
     * @var Object
     */
    public $_sorted;

    /**
     * Selected columns
     * @var array
     */
    public $_columns;

    /**
     * Extract necessary information from Camelot XML Packet
     *
     * @param String $CamelotXml
     * @param Boolean $status 1 is true, 0 is false (not used in this context but is there for standardisation)
     * @param string|array $colums Semicolon separated string or array
     * @return Object
     */
    public function get_list_data($CamelotXml, $status = 1, $colums = null) {

        if (!$CamelotXml) {
            return new CamelotException(null, 400);
        }

        // Check if Camelot XML is valid, if not lets throw the value of the argument
        @$a = simplexml_load_string($CamelotXml);
        if ($a === FALSE) {
            return new CamelotException(null, 409);
        }

        unset($a);

        if ($colums) {
            // split columns (if any) into array
            if (is_string($colums)) {
                $this->_columns = explode(";", $colums);
            } else if (is_array($colums)) {
                $this->_columns = $colums;
            }
        } else {
            // set something to prevent warning when loading schema
            $this->_columns = null;
        }

        // Load the schema
        $schema = $this->get_schema_from_camelot_xml($CamelotXml);

        // Load the list content (as array)
        $content = $this->get_content_from_camelot_xml($CamelotXml);

        // Create CamelotXmlSorting instance
        $CamelotXmlSorting = new CamelotXmlSorting();

        // Create the schema sorter
        $this->initiate_sorted();


        // Sort schema
        $sortedSchema = $CamelotXmlSorting->sort_schema($schema, $this->_columns);
        if ($sortedSchema instanceof CamelotException) {
            return $sortedSchema;
        } else {
            $this->_sorted->_schema = $sortedSchema;
        }

        // Sort the list content
        if (count($content) > 0) {
            // Sort content
            $sorted = $CamelotXmlSorting->sort_content($content, $sortedSchema->attribute_name);
            if ($sorted instanceof CamelotException) {
                return $sorted;
            } else {
                $this->_sorted->_content = $sorted;
            }
        } else {
            $this->_sorted->_content = null;
        }

        // Kill CamelotXmlSorting instance
        unset($CamelotXmlSorting);

        return $this;
    }

    /**
     * Initiate the sorting array to not trigger any strict checks
     */
    private function initiate_sorted() {
        if (!isset($this->_sorted)) {
            $this->_sorted = new StdClass();
        }
        if (!isset($this->_sorted->_schema)) {
            $this->_sorted->_schema = new StdClass();
        }
    }

    /**
     * Returns the schema from the loaded Camelot XML structure
     *
     * @param string $CamelotXml XML formatted SharePoint content
     * @return string XML format
     */
    private function get_schema_from_camelot_xml($CamelotXml) {
        $packet = simplexml_load_string($CamelotXml);
        $schema = $packet->Schema->asXML();
        return $schema;
    }

    /**
     * Returns an array with the content
     *
     * @param string $CamelotXml XML formatted SharePoint content
     * @return array Each row in the array contains the xml of the SharePoint list row
     */
    private function get_content_from_camelot_xml($CamelotXml) {
        $packet = simplexml_load_string($CamelotXml);
        $content = $packet->Content->Row;

        // Transform the data to the same structure as we would have if it came
        // from a database (stored in the camelot_content table)
        $data = array();
        foreach ($content as $key => $value) {
            $temp = $value->asXML();
            $data[] = $temp;
        }

        return $data;
    }

}

?>
